package com.ys.otms.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

public class CommonInterceptor implements HandlerInterceptor {

	private List<String> excludedUrls;
	
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {

    }

    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {

    }

    @SuppressWarnings("unchecked")
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {

        String requestUrl = request.getRequestURI();
		for(String s : excludedUrls) {
			if(requestUrl.endsWith(s)) {
				return true;
			}
		}

        HttpSession session = request.getSession();
		if (null != session.getAttribute("SESSION_USER_ID")) {
		    return true;
        }
        else {
		    response.sendRedirect("toLogin.do");
            return false;
        }
    }

    public List<String> getExcludedUrls() {
		return excludedUrls;
	}

	public void setExcludedUrls(List<String> excludedUrls) {
		this.excludedUrls = excludedUrls;
	}

}
